// Decompiled by Jad v1.5.7f. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   RTEFileHandler.java

package com.iphaeton.lms.course.scorm12.runtime.core;

import com.iphaeton.lms.course.scorm12.datamodel.SCODataManager;
import com.iphaeton.lms.course.scorm12.datamodel.cmi.*;
import com.iphaeton.persistence.spring.CourseDBService;
import java.io.*;
import java.sql.*;
import org.apache.log4j.Logger;

public class RTEFileHandler
{

    public RTEFileHandler()
    {
        userID = new String();
        courseID = new String();
        scoID = new String();
        location = new String();
        masteryScore = new String();
        parameterString = new String();
        lessonStatus = new String();
        prerequisites = new String();
        exit = new String();
        entry = new String();
        dataFromLMS = new String();
        maxTimeAllowed = new String();
        timeLimitAction = new String();
        sequence = 0;
        sampleRTERoot = "\\SampleRTEFiles";
    }

    public void setUserID(String idIn)
    {
        userID = idIn;
    }

    public void setCourseID(String idIn)
    {
        courseID = idIn;
    }

    public void printRTEFileHandler()
    {
        if(logger.isDebugEnabled())
        {
            logger.debug("userID:          " + userID);
            logger.debug("courseID:        " + courseID);
            logger.debug("SampleRTERoot:   " + sampleRTERoot);
        }
    }

    public void initializeCourseFiles()
    {
        Connection conn = null;
        PreparedStatement rtestmtSelectItem = null;
        PreparedStatement stmtInsertUserSCO = null;
        try
        {
            logger.debug("****GOT IN INITIALIZECOURSEFILES****");
            String userDir = sampleRTERoot + "\\" + userID + "\\" + courseID;
            File theRTESCODataDir = new File(userDir);
            if(!theRTESCODataDir.isDirectory())
                theRTESCODataDir.mkdirs();
            else
                logger.debug("In RTEFileHandler user directory already exists for new course???");
            String sqlSelectItem = "SELECT * FROM ItemInfo WHERE CourseID = ?";
            String sqlInsertUserSCO = "INSERT INTO UserSCOInfo (UserID, CourseID, SCOID, Launch, ParameterString, LessonStatus, Prerequisites, ExitStatus, Entry, MasteryScore, Sequence, Type) VALUES(?,?,?,?,?,?,?,?,?,?,?,?)";
            conn = CourseDBService.getConnection();
            rtestmtSelectItem = conn.prepareStatement(sqlSelectItem);
            stmtInsertUserSCO = conn.prepareStatement(sqlInsertUserSCO);
            if(logger.isDebugEnabled())
            {
                logger.debug("about to call item in RTEFile");
                logger.debug("userID: " + userID);
                logger.debug("courseID: " + courseID);
            }
            ResultSet itemRS = null;
            synchronized(rtestmtSelectItem)
            {
                rtestmtSelectItem.setString(1, courseID);
                itemRS = rtestmtSelectItem.executeQuery();
            }
            logger.debug("call to itemRS is complete");
            while(itemRS.next()) 
            {
                String type = itemRS.getString("Type");
                if(type.equals("sco") || type.equals("asset"))
                {
                    scoID = itemRS.getString("Identifier");
                    location = itemRS.getString("Launch");
                    masteryScore = itemRS.getString("MasteryScore");
                    prerequisites = itemRS.getString("Prerequisites");
                    parameterString = itemRS.getString("ParameterString");
                    dataFromLMS = itemRS.getString("DataFromLMS");
                    maxTimeAllowed = itemRS.getString("MaxTimeAllowed");
                    timeLimitAction = itemRS.getString("TimeLimitAction");
                    sequence = itemRS.getInt("Sequence");
                    String RTESCODataFile = userDir + "\\" + scoID;
                    File theRTESCODataFile = new File(RTESCODataFile);
                    SCODataManager rteSCOData = new SCODataManager();
                    rteSCOData = initSCOData();
                    FileOutputStream fo = new FileOutputStream(RTESCODataFile);
                    ObjectOutputStream out = new ObjectOutputStream(fo);
                    out.writeObject(rteSCOData);
                    out.close();
                    logger.debug("RTEFileHandler created SCO file for: " + scoID);
                    synchronized(stmtInsertUserSCO)
                    {
                        stmtInsertUserSCO.setString(1, userID);
                        stmtInsertUserSCO.setString(2, courseID);
                        stmtInsertUserSCO.setString(3, scoID);
                        stmtInsertUserSCO.setString(4, location);
                        stmtInsertUserSCO.setString(5, parameterString);
                        stmtInsertUserSCO.setString(6, "not attempted");
                        stmtInsertUserSCO.setString(7, prerequisites);
                        stmtInsertUserSCO.setString(8, "");
                        stmtInsertUserSCO.setString(9, "ab-initio");
                        stmtInsertUserSCO.setString(10, masteryScore);
                        stmtInsertUserSCO.setInt(11, sequence);
                        stmtInsertUserSCO.setString(12, type);
                        stmtInsertUserSCO.executeUpdate();
                    }
                } else
                {
                    logger.debug("NOT A SCO OR ASSET");
                }
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                rtestmtSelectItem.close();
                stmtInsertUserSCO.close();
                conn.close();
            }
            catch(SQLException e)
            {
                e.printStackTrace();
            }
        }
        return;
    }

    public void deleteCourseFiles()
    {
        try
        {
            String userDir = sampleRTERoot + "\\" + userID + "\\" + courseID;
            File theRTESCODataDir = new File(userDir);
            File scoFiles[] = theRTESCODataDir.listFiles();
            for(int i = 0; i < scoFiles.length; i++)
                scoFiles[i].delete();

            theRTESCODataDir.delete();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    private SCODataManager initSCOData()
    {
        SCODataManager tmpSCOData;
        tmpSCOData = new SCODataManager();
        Connection conn = null;
        PreparedStatement stmtSelectUser = null;
        try
        {
            String sqlSelectUser = "SELECT * FROM UserInfo WHERE UserID = ?";
            conn = CourseDBService.getConnection();
            stmtSelectUser = conn.prepareStatement(sqlSelectUser);
            logger.debug("getting user info");
            ResultSet userRS = null;
            synchronized(stmtSelectUser)
            {
                stmtSelectUser.setString(1, userID);
                userRS = stmtSelectUser.executeQuery();
            }
            String firstName = new String();
            String lastName = new String();
            if(userRS.next())
            {
                logger.debug("in if to get names");
                firstName = userRS.getString("FirstName");
                lastName = userRS.getString("LastName");
            }
            String studentName = lastName + ", " + firstName;
            logger.debug("student name: " + studentName);
            CMIScore tmpScore = new CMIScore();
            tmpScore.setRaw("0");
            tmpScore.setMax("0");
            tmpScore.setMin("0");
            CMICore tmpCore = new CMICore();
            tmpCore.setStudentId(userID);
            tmpCore.setStudentName(studentName);
            tmpCore.setLessonLocation(location);
            tmpCore.setCredit("credit");
            tmpCore.setLessonStatus("not attempted");
            tmpCore.setEntry("ab-initio");
            tmpCore.setTotalTime("00:00:00.0");
            tmpCore.setLessonMode("normal");
            tmpCore.setExit("");
            tmpCore.setScore(tmpScore);
            tmpSCOData.setCore(tmpCore);
            CMISuspendData tmpSuspendData = new CMISuspendData();
            tmpSuspendData.setSuspendData("");
            tmpSCOData.setSuspendData(tmpSuspendData);
            CMILaunchData tmpLaunchData = new CMILaunchData();
            tmpLaunchData.setLaunchData(dataFromLMS);
            tmpSCOData.setLaunchData(tmpLaunchData);
            CMICommentsFromLms tmpComments = new CMICommentsFromLms("");
            tmpSCOData.setCommentsFromLMS(tmpComments);
            CMIStudentData tmpStudentData = new CMIStudentData();
            tmpStudentData.setMasteryScore(masteryScore);
            tmpStudentData.setMaxTimeAllowed(maxTimeAllowed);
            tmpStudentData.setTimeLimitAction(timeLimitAction);
            tmpSCOData.setStudentData(tmpStudentData);
            CMIStudentPreference tmpStudentPreference = new CMIStudentPreference();
            tmpStudentPreference.setAudio("3");
            tmpStudentPreference.setLanguage("English");
            tmpStudentPreference.setSpeed("4");
            tmpStudentPreference.setText("10");
            tmpSCOData.setStudentPreference(tmpStudentPreference);
        }
        catch(SQLException e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                stmtSelectUser.close();
                conn.close();
            }
            catch(SQLException e)
            {
                e.printStackTrace();
            }
        }
        return tmpSCOData;
    }

    private static transient Logger logger;
    private String userID;
    private String courseID;
    private String sampleRTERoot;
    private String scoID;
    private String location;
    private String masteryScore;
    private String parameterString;
    private String lessonStatus;
    private String prerequisites;
    private String exit;
    private String entry;
    private String dataFromLMS;
    private String maxTimeAllowed;
    private String timeLimitAction;
    private int sequence;
    static Class class$0; /* synthetic field */

    static 
    {
        logger = Logger.getLogger(com.iphaeton.lms.course.scorm12.runtime.core.RTEFileHandler.class.getName());
    }
}
